﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Text;
using System.Security.Cryptography;
using System.IO;

using CE.Global;
using CE.Database;
using CE.Configuration;

public partial class savings_result : MyLanguageBasePage
{
    protected override void InitializeCulture()
    {
        String selectedCulture = CE.Global.General.SetCulture();
        base.InitializeCulture();
    }

    string selectedLanguage;
    protected void Page_Load(object sender, EventArgs e)
    {
        string _strCXLive = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.IsCXLive");
        if (_strCXLive == "0")
        {
            if (Session[CE.Global.General.Guest] == null)
            {
                Response.Redirect("~/Default");
            }

        }
        Page.Header.Title = Resources.FWD.Results;
        HtmlLink css = new HtmlLink();
        css.Href = Resources.FWD.CssSavingsUrl;
        css.Attributes["rel"] = "stylesheet";
        css.Attributes["type"] = "text/css";
        css.Attributes["media"] = "all";
        Page.Header.Controls.Add(css);


        if (!Page.IsPostBack)
        {
            ibtnLanguage.ImageUrl = Resources.FWD.ImgLanguageUrl;
            hplHome.NavigateUrl = CE.Global.General.GetUrlHome("/", Page);

            string _redirectUrl = "";
            _redirectUrl = CE.Global.General.GetUrl("/savings/life-insurance", Page);
            //_redirectUrl = _redirectUrl.Replace("en/en/", "en/");
            aHome.HRef = _redirectUrl;
            aTextHome.HRef = _redirectUrl;

            txtEmailMeName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailMeName.ClientID + "','" + rfvEmailMeName.ClientID + "','" + revEmailMeName.ClientID + "');");
            txtEmailMeMobile.Attributes.Add("onblur", "ShowValidationEffect('" + txtEmailMeMobile.ClientID + "','" + revEmailMeMobile.ClientID + "');");
            txtEmailMeEmail.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtEmailMeEmail.ClientID + "','" + rfvEmailMeEmail.ClientID + "','" + revEmailMeEmail.ClientID + "');");

            txtCallMeName.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCallMeName.ClientID + "','" + rfvCallMeName.ClientID + "','" + revCallMeName.ClientID + "');");
            txtCallMeMobile.Attributes.Add("onblur", "ShowValidationEffectTwo('" + txtCallMeMobile.ClientID + "','" + rfvCallMeMobile.ClientID + "','" + revCallMeMobile.ClientID + "');");
            txtCallMeEmail.Attributes.Add("onblur", "ShowValidationEffect('" + txtCallMeEmail.ClientID + "','" + revCallMeEmail.ClientID + "');");
            ddlCallMeTime.Attributes.Add("onblur", "ShowValidationEffect('" + ddlCallMeTime.ClientID + "','" + rfvCallMeTime.ClientID + "');");

            Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);
            string rq_fwdid = HttpUtility.ParseQueryString(theRealURL.Query).Get("val");
            if (rq_fwdid != null && rq_fwdid != string.Empty)
            {
                int _fwdid = 0;
                if (!int.TryParse(rq_fwdid.Trim(), out _fwdid))
                {
                    _fwdid = 0;
                }
                hdnSelectFWD.Value = _fwdid.ToString();
                if (_fwdid > 0)
                {
                    LoadAmount(_fwdid);
                }
            }
        }
    }

    protected void ibtnLanguage_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        selectedLanguage = string.Empty;

        if (hdnLanguage.Value != string.Empty)
        {
            Session["Language"] = hdnLanguage.Value;
        }

        if (Session["Language"] != null)
        {
            selectedLanguage = Session["Language"].ToString();
        }
        else
        {
            selectedLanguage = "th-TH";
            Session["Language"] = selectedLanguage;
        }

        if (selectedLanguage == "th-TH")
        {
            Session["Language"] = "en-US";
        }
        else
        {
            Session["Language"] = "th-TH";
        }

        hdnLanguage.Value = selectedLanguage;
        if (selectedLanguage == "en-US")
        {
            Response.Redirect(Request.UrlReferrer.AbsoluteUri.Replace("/en", ""));
        }
        else
        {
            string[] _arrList = Request.UrlReferrer.AbsoluteUri.Split('/');
            string _redirectUrl = "";
            _arrList[3] = "en/" + _arrList[3];
            for (int i = 0; i < _arrList.Length; i++)
            {

                if (i != _arrList.Length - 1)
                {
                    _redirectUrl += _arrList[i].ToString() + "/";
                }
                else
                {
                    _redirectUrl += _arrList[i].ToString();
                }

            }
            if (_arrList.Length == 4 && _redirectUrl.EndsWith("/"))
            {
                _redirectUrl += "home";
            }

            Response.Redirect(_redirectUrl);
        }
    }

    protected void btnCallMe_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate("callme");
            if (Page.IsValid)
            {
                Int64 _result = SaveSlectedFWD("CALLME");
                if (_result > 0)
                {
                    ClearFields();
                    string _redirectUrl = "";
                    _redirectUrl = CE.Global.General.GetUrl("~/savings/thankyou", Page);
                    Response.Redirect(_redirectUrl + "?type=call&fwd=yes");
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    protected void btnEmailMe_Click(object sender, EventArgs e)
    {
        try
        {
            Page.Validate("emailme");
            if (Page.IsValid)
            {
                Int64 _result = SaveSlectedFWD("EMAILME");
                if (_result > 0)
                {
                    ClearFields();
                    string _redirectUrl = "";
                    _redirectUrl = CE.Global.General.GetUrl("~/savings/thankyou", Page);
                    Response.Redirect(_redirectUrl + "?type=email&fwd=yes");
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    public void LoadAmount(Int32 FWDId)
    {
        try
        {
            lblAnnualSavings.Text = "";
            lblTotalSavings.Text = "";
            lblMaturityBenefit.Text = "";
            MyDatabase _db = new MyDatabase();
            DataTable _dtblFWDAmount = new DataTable();
            SortedList _spList = new SortedList();
            _spList.Add("@FWDId", FWDId);
            _dtblFWDAmount = _db.GetDataTableBySP("sprocFWDPremiumSelectedItem", _spList);
            if (_dtblFWDAmount != null && _dtblFWDAmount.Rows.Count > 0)
            {
                if (_dtblFWDAmount.Rows[0]["AnnualSavingsDisp"] != null && _dtblFWDAmount.Rows[0]["AnnualSavingsDisp"].ToString() != string.Empty)
                {
                    lblAnnualSavings.Text = _dtblFWDAmount.Rows[0]["AnnualSavingsDisp"].ToString();
                }
                if (_dtblFWDAmount.Rows[0]["FiveYearSavingsDisp"] != null && _dtblFWDAmount.Rows[0]["FiveYearSavingsDisp"].ToString() != string.Empty)
                {
                    lblTotalSavings.Text = _dtblFWDAmount.Rows[0]["FiveYearSavingsDisp"].ToString();
                }
                if (_dtblFWDAmount.Rows[0]["MaturityBenefitDisp"] != null && _dtblFWDAmount.Rows[0]["MaturityBenefitDisp"].ToString() != string.Empty)
                {
                    lblMaturityBenefit.Text = _dtblFWDAmount.Rows[0]["MaturityBenefitDisp"].ToString();
                }
            }
        }
        catch (Exception ex)
        {
        }
    }

    public Int64 SaveSlectedFWD(string RequesrType)
    {
        try
        {
            string _strName = "";
            string _strEmail = "";
            string _strMobile = "";
            Int16 _sintCallTime = -1;
            MyDatabase _dbUpdate = new MyDatabase();
            SortedList _spList = new SortedList();

            if (RequesrType == "CALLME")
            {
                _strName = Server.HtmlEncode(txtCallMeName.Text.Trim());
                _strEmail = Server.HtmlEncode(txtCallMeEmail.Text.Trim());
                _strMobile = Server.HtmlEncode(txtCallMeMobile.Text.Trim());
                _sintCallTime = Convert.ToInt16(ddlCallMeTime.Text);
            }
            else if (RequesrType == "EMAILME")
            {
                _strName = Server.HtmlEncode(txtEmailMeName.Text.Trim());
                _strEmail = Server.HtmlEncode(txtEmailMeEmail.Text.Trim());
                _strMobile = Server.HtmlEncode(txtEmailMeMobile.Text.Trim());
            }
            string _spName = "sprocFWDCustomerRequestInsertUpdateSingleItem";

            _spList.Add("@RequestId", 0);
            _spList.Add("@FWDId", Convert.ToInt32(hdnSelectFWD.Value));
            _spList.Add("@RequestType", RequesrType);
            _spList.Add("@Name", _strName);
            _spList.Add("@Email", _strEmail);
            _spList.Add("@Mobile", _strMobile);
            _spList.Add("@CallTime", _sintCallTime);

            object _retObject = _dbUpdate.ExecuteSP(_spName, _spList);
            return Convert.ToInt64(_retObject);
        }
        catch (Exception ex)
        {
            return -1;
        }
    }

    public void ClearFields()
    {
        txtCallMeName.Text = "";
        txtCallMeEmail.Text = "";
        txtCallMeMobile.Text = "";
        ddlCallMeTime.Text = "-1";
        txtEmailMeName.Text = "";
        txtEmailMeEmail.Text = "";
        txtEmailMeMobile.Text = "";
    }

}
